import {Injectable} from '@angular/core';
import {HttpHelper} from "./HttpHelper";
import {ProductionProcess, TimeSheet} from "./domains";


@Injectable()
export class ProductionProcessService{
    constructor(private helper:HttpHelper){

    }
    processes():Promise<ProductionProcess[]>{
        return this.helper.getJson<ProductionProcess[]>("production-process");
    }
    process(processId):Promise<ProductionProcess>{
        return this.helper.getJson<ProductionProcess>("production-process/"+processId);
    }
    liableProcesses(page?,size?):Promise<ProductionProcess[]>{
        let url = "production-process/liable";
        if (page&&size) {
            url = url + `/?page=${page}&size=${size}`
        }
        return this.helper.getJson<ProductionProcess[]>(url);
    }
    workerProcesses(page?,size?):Promise<ProductionProcess[]>{
        let url = "production-process/worker";
        if (page&size) {
            url = url +`/?page=${page}&size=${size}`;
        }
        return this.helper.getJson<ProductionProcess[]>(url);
    }
    addProcess(bill:number,manufacture:number,quantity:number,process:number,liable:number,workshop:number,expectStartTime?:any,expectEndTime?:any,workers?:Array<any>){
        let data=`bill=${bill}&manufacture=${manufacture}&quantity=${quantity}
        &process=${process}&liable=${liable}&workshop=${workshop}`;
        if (workers) {
            workers.forEach(worker => {
                data+=`&workers=${worker}`;
            })
        }
        if (expectStartTime){
            data = data+`&ex-start=${expectStartTime}`;
        } 
        if (expectEndTime) {
            data = data + `&ex-end=${expectEndTime}`;
        }
        return this.helper.postToJson("production-process",data,this.helper.contentHeaderEnum.formHeader);
    }
    beginProcess(processId:number):Promise<boolean>{
        let data=`operator=begin&processId=${processId}`;
        return this.helper.putToBoolean("production-process",data,this.helper.contentHeaderEnum.formHeader);
    }
    endProcess(processId:number):Promise<boolean>{
        let data=`operator=end&processId=${processId}`;
        return this.helper.putToBoolean("production-process",data,this.helper.contentHeaderEnum.formHeader);
    }
    updateProcessManager(process): Promise<ProductionProcess> {
        let url = `production-process`;
        return this.helper.putToJson<ProductionProcess>(url,process);
    }

}